package com.lnlyj.media.data.load;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

import com.lnlyj.media.LnlyjSelector;
import com.lnlyj.media.R;
import com.lnlyj.media.data.AlbumItem;

import java.util.ArrayList;
import java.util.List;

/**
 * wanglei 2020-04-16
 */
public class AlbumLoader extends AsyncTask {

    public static final String TAG = "AlbumLoader";

    private ContentResolver resolver;

    private OnAlbumLoaderCallback callback;

    public AlbumLoader(ContentResolver resolver) {
        this.resolver = resolver;
    }

    @Override
    protected Object doInBackground(Object[] objects) {

        Cursor cursor = null;
        try {
            List<AlbumItem> list = new ArrayList<>();

            QueryConfig config = QueryFactory.getMediaConfig(-1);

            cursor = resolver.query(config.uri, config.projection, config.selection, config.selectionArgs, config.sortOrder);

            AlbumItem all = new AlbumItem();

            all.setName(LnlyjSelector.getInstance().getApplication().getResources().getString(R.string.media_text_all_photo_video));
            all.setConfig(config);

            if (cursor != null && cursor.moveToFirst()) {
                all.setCount(cursor.getCount());
                all.setIcon(cursor.getString(cursor.getColumnIndex("_data")));
                all.setIconId(cursor.getInt(cursor.getColumnIndex("_id")));
                list.add(all);
            }

            config = QueryFactory.getAlbumConfig();

            cursor = resolver.query(config.uri, config.projection, config.selection, config.selectionArgs, config.sortOrder);

            if (cursor != null) {
                while (cursor.moveToNext()) {
                    list.add(newAlbumData(resolver, cursor, config.uri));
                }
            }

            if (callback != null) {
                callback.onAlbumLoaderFinish(list);
            }
        } catch (Exception e) {
            Log.w(TAG, e.getMessage());
        } finally {
            if (cursor != null) {
                cursor.close();
                cursor = null;
            }
        }

        return null;
    }

    private AlbumItem newAlbumData(ContentResolver resolver, Cursor cursor, Uri uri) {
        AlbumItem item = new AlbumItem();

        long bucketId = cursor.getLong(cursor.getColumnIndex("bucket_id"));
        String name = cursor.getString(cursor.getColumnIndex("bucket_display_name"));

        item.setConfig(QueryFactory.getMediaConfig(bucketId));
        item.setName(name);
        item.setBucketId(bucketId);

        Cursor data = null;
        try {
            data = resolver.query(uri, new String[]{"_id, _data, count(*) as count"}, "bucket_id=? and (media_type=1 or media_type=3)", new String[]{cursor.getString(cursor.getColumnIndex("bucket_id"))}, "date_modified DESC");
            if (data != null && data.moveToFirst()) {
                item.setIconId(data.getInt(0));
                item.setIcon(data.getString(1));
                item.setCount(data.getInt(2));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (data != null) {
                data.close();
                data = null;
            }
        }
        return item;
    }


    public void setCallback(OnAlbumLoaderCallback callback) {
        this.callback = callback;
    }

    public interface OnAlbumLoaderCallback {
        void onAlbumLoaderFinish(List<AlbumItem> list);
    }
}
